home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Power 1997 December
/
MACPOWER-1997-12.ISO.7z
/
MACPOWER-1997-12.ISO
/
AMUG
/
PROGRAMMING
/
Raven 1.2.sit
/
Raven 1.2
/
Source
/
Foundation
/
Common
/
ZReferenceCounted.h
< prev
next >
Wrap
Text File
|
1996-10-28
|
2KB
|
87 lines
/*
* File: ZReferenceCounted.h
* Summary: Mixin class to help with reference counting.
* Written by: Jesse Jones
*
* Copyright ゥ 1996 Jesse Jones.
* For conditions of distribution and use, see copyright notice in ZTypes.h
*
* Abstract: Reference counting is basicly a weak form of garbage collection: it allows
* one object to be shared by other objects. When there are no more references
* to the first object it is automatically deleted.
*
* Using reference counting reduces memory requirements and also always heavy
* weight objects to be quickly copied.
*
* Usage: Reference counting is typically implemented using the envelope/letter pattern.
* The envelope is the class users see. The envelope has a pointer to the letter
* which does all the work. Letters can belong to more than one envelope. Here's
* an example of how this works:
*
* TString::~TString()
* {
* mRefPtr->RemoveReference(); // when refCount goes to zero letter deletes itself
* }
*
* TString::TString(const char* str)
* {
* mRefPtr = new TStringRef(str);
* }
*
* TString::TString(const TString& rt)
* {
* mRefPtr = rt.mRefPtr; // note that the pointer is copied -- not the data!
* mRefPtr->AddReference();
* }
*
* TString& TString::operator=(const TString& rt)
* {
* if (mRefPtr != rt.mRefPtr) {
* mRefPtr->RemoveReference();
*
* mRefPtr = rt.mRefPtr;
* mRefPtr->AddReference();
* }
* return *this;
* }
*
* Change History (most recent first):
*
* <-> 1/14/96 JDJ Created.
*/
#pragma once
#include <ZDebug.h>
#include <ZTypes.h>
// ===================================================================================
// class MReferenceCounted
// ===================================================================================
class MReferenceCounted {
public:
virtual ~MReferenceCounted() = 0;
explicit MReferenceCounted(long refCount = 1);
virtual void AddReference();
virtual void RemoveReference();
// Deletes the object if no one is referencing it.
long GetRefCount() const {return mRefCount;}
protected:
virtual void OnFirstReference();
// Defaults to nothing.
virtual void OnLastReference();
// Defaults to deleting the object.
private:
long mRefCount;
};